﻿package fui.FUIComponents{
	import flash.events.*;
	import flash.display.Sprite;
	import fl.controls.*;
	import fl.containers.ScrollPane;
	import fui.FUIControllers.*;
	import fui.FUIData.IDBClient;
	import fui.FUIData.DBClientV2;
	
	public class CreateGame extends Sprite implements IComponent{
		private var _comName:String = "";
		private var _uis:UISession;
		private var _DBCon:IDBClient;
		private var leftTab:int = 10;
		
		private var _datelbl:Label;
		private var _categlbl:Label;
		private var _tmpllbl:Label;
		private var _isprivatelbl:CheckBox;
		private var _usrTitlelbl:Label;
		private var _startDate:TextInput;
		private var _categoryList:ComboBox;
		private var _tmplViewPane:ScrollPane;
		private var startCreateBtn:Button;
		
		private var _templateList:BoardTemplateView;
		private var tt:Label;
		private var _subbutton:Button;
		private var _cnclbutton:Button;
		
		var dt:Object;
		
		public function CreateGame(dbCon:IDBClient)
		{
			_comName = "Create Game";
			this._DBCon = dbCon;
			tt = new Label();
			tt.width = 300;
			addChild(tt);
			tt.x = 60;
			tt.y = 100;
			tt.text = " Welcom! Here you can create New Game.";
			startCreateBtn = new Button();
			startCreateBtn.x = 200;
			startCreateBtn.y = 200;
			startCreateBtn.label = "Create new Game";
			startCreateBtn.width = 150;
			startCreateBtn.addEventListener(MouseEvent.CLICK, onCreateNewGame);
			addChild(startCreateBtn);
			
		}
		
		private function onCreateNewGame(ev:Event):void
		{
			
			this._DBCon.addEventListener(Event.COMPLETE, dataLoaded);
			this._DBCon.CallStoredProcedure("verify_user_by_name","ok");
//			createView();
		}
		
		
		
		public function onCancel(ev:Event):void
		{
			hide_component();
		}
		
		private function dataLoaded(ev:Event):void
		{
			dt = this._DBCon.CallResponse;
			trace (dt.rc);
			if(dt.rc == true)
			{
				removeChild(tt);
				startCreateBtn.removeEventListener(MouseEvent.CLICK, onCreateNewGame);
				removeChild(startCreateBtn);
				createView();
			}
		}
		
		public function createView():void
		{
			_datelbl = new Label();
			_datelbl.text = "Insert Game start date";
			_datelbl.x = leftTab;
			_datelbl.y = 20;
			_datelbl.width = 200;
			addChild(_datelbl);
			_startDate = new TextInput();
			_startDate.x = leftTab + 210;
			_startDate.y = 20;
			addChild(_startDate);
			_categlbl = new Label();
			_categlbl.text = "Select Category of Game: ";
			_categlbl.x = leftTab;
			_categlbl.y = 60;
			_categlbl.width = 200;
			addChild(_categlbl);
			_categoryList = new ComboBox();
			_categoryList.x = leftTab + 210;
			_categoryList.y = 60;
			_categoryList.width = 200;
			addChild(_categoryList);
			_tmpllbl = new Label();
			_tmpllbl.text = "Select Template of Game Board";
			_tmpllbl.x = leftTab;
			_tmpllbl.y = 100;
			_tmpllbl.width = 200;
			addChild(_tmpllbl);
			_isprivatelbl = new CheckBox();
			_isprivatelbl.label = "Check if this game is private                      ";
			_isprivatelbl.x = leftTab;
			_isprivatelbl.y = 320;
			_isprivatelbl.width = 230;
			_isprivatelbl.labelPlacement = "left";
			addChild(_isprivatelbl);
			
			
			_subbutton = new Button();
			_cnclbutton = new Button();
			_subbutton.x = 150;
			_subbutton.y = 500;
			_subbutton.label = "Create";
			_cnclbutton.x = 350;
			_cnclbutton.y = 500;
			_cnclbutton.label = "Cancel";
			_cnclbutton.addEventListener(MouseEvent.CLICK, onCancel);
			addChild(_subbutton);
			addChild(_cnclbutton);
			createTemplateView();
		}
		
		private function createTemplateView():void
		{
			_tmplViewPane = new ScrollPane();
			_tmplViewPane.x = leftTab + 210;
			_tmplViewPane.y = 100;
			_tmplViewPane.height = 182;
			_tmplViewPane.width = 310;
			
			
			var dataarr:Array = new Array();
			dataarr.push(dt);
			
			_templateList = new BoardTemplateView(dataarr);
			

			_tmplViewPane.addChild(_templateList);
			addChild(_tmplViewPane);
		}
		public function set_xml_source(xml_path:String):void
		{
			//TODO:
		}
		public function set_session(uis:UISession):void
		{
			this._uis = uis;
		}
		public function get_component_title():String
		{
			return this._comName;
		}
        public function show_component():void
		{
			//TODO:
			visible = true;
		}
		
		public function hide_component():void
		{
			//TODO:
			visible = false;
		}
		public function if_need_auth():Boolean
		{
			//TODO:
			return true;
		}
		public function self_remove():void
		{
			//TODO:
		}
	}
}